package com.imau.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imau.bean.Teacher;
import com.imau.common.dataSourceTool.SqlSessionFactoryUtil;
import com.imau.common.entiry.PageQuery;
import com.imau.common.utils.StringUtils;
import com.imau.mapper.TeacherMapper;
import com.imau.service.TeacherService;
import org.apache.ibatis.session.SqlSession;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

public class TeacherServiceImp extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    //操作数据库的session
    private SqlSession session;


    public TeacherServiceImp()
    {
        //父类 ServiceImpl属性，自动注入
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        this.baseMapper = session.getMapper(TeacherMapper.class);
    }

    @Override
    public Teacher queryById(Serializable id) {

        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);

        return this.baseMapper.selectById(id); //super.getById(id);
    }

    @Override
    public List<Teacher> queryList(Teacher entity) {
        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        //1.构建查询条件
        LambdaQueryWrapper<Teacher> lqw = buildQueryWrapper(entity);

        //2.调用方法

        return super.list(lqw);
    }

    @Override
    public Page<Teacher> queryPageList(Teacher entity, PageQuery pageQuery) {

        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        //1.构建查询条件
        LambdaQueryWrapper<Teacher> lqw = buildQueryWrapper(entity);


        return super.page(pageQuery.build(),lqw);
    }

    @Override
    public boolean updateByEntity(Teacher entity) {

        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return this.updateById(entity);
    }

    @Override
    public boolean saveEntity(Teacher entity) {

        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return super.save(entity);
    }

    @Override
    public boolean removeWithById(Serializable id) {

        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return  this.removeById(id);
    }

    @Override
    public boolean removeWithValidByIds(Collection<?> ids) {
        session= SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return  this.removeBatchByIds(ids);
    }

    @Override
    public LambdaQueryWrapper<Teacher> buildQueryWrapper(Teacher bo) {

        //根据字段类别
        LambdaQueryWrapper<Teacher> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTid() != null, Teacher::getTid, bo.getTid());
        lqw.eq(bo.getWorkTime() != null, Teacher::getWorkTime, bo.getWorkTime());
        lqw.eq(StringUtils.isNotBlank(bo.getNational()), Teacher::getNational, bo.getNational());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), Teacher::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), Teacher::getIdCard, bo.getIdCard());
        lqw.eq( bo.getBirthday()!=null, Teacher::getBirthday, bo.getBirthday());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Teacher::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getAddress()), Teacher::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), Teacher::getPhone, bo.getPhone());
        lqw.like(StringUtils.isNotBlank(bo.getAchievements()), Teacher::getAchievements, bo.getAchievements());
        lqw.like(StringUtils.isNotBlank(bo.getGraduation_school()), Teacher::getGraduation_school, bo.getGraduation_school());
        lqw.like(StringUtils.isNotBlank(bo.getJobTitle()), Teacher::getJobTitle, bo.getJobTitle());
        lqw.like(StringUtils.isNotBlank(bo.getPartisan()), Teacher::getPartisan, bo.getPartisan());
        lqw.like( bo.getGraduationDate() != null, Teacher::getGraduationDate, bo.getGraduationDate());

        lqw.orderByAsc(Teacher::getTid);
        return lqw;

    }
}
